package smedi.ismedi.mobile.bff.controller.report;


import com.smedi.ismedi.report.api.feign.RemoteReportExportFeign;
import com.smedi.ismedi.report.api.feign.RemoteReportFolwFeginApi;
import com.smedi.ismedi.report.api.feign.RemoteReportMobileApi;
import com.smedi.ismedi.report.api.feign.RemoteReportPalnApi;
import com.smedi.ismedi.report.api.model.dto.AppointVO;
import com.smedi.ismedi.report.api.model.dto.FlowPassDTO;
import com.smedi.ismedi.report.api.model.dto.RejectVO;
import com.smedi.ismedi.report.api.model.vo.NoticeVo;
import com.smedi.ismedi.report.api.model.vo.OtherFillVO;
import com.smedi.ismedi.report.api.model.vo.ReportExportVO;
import com.smedi.ismedi.web.starter.common.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RequestMapping(value = "/mobilebff/v1/flow")
@RestController
@Api(tags = {"填报流程"})
public class RemoteReportFolwController {
    @Autowired
    private RemoteReportFolwFeginApi remoteReportFolwFeginApi;

    @Autowired
    private RemoteReportMobileApi remoteReportMobileApi;

    /**
     * 流程提交
     * @param flowPassDTO dto
     * @return ResponseData<String>
     */
    @ApiOperation( value = "填报流程提交",nickname = "流程提交" )
    @PostMapping("/pass")
     public  ResponseData<String> pass(@RequestBody FlowPassDTO flowPassDTO){
        return  remoteReportFolwFeginApi.pass(flowPassDTO);
    }

    /**
     * 驳回
     */
    @PostMapping("/reject")
    @ApiOperation( value = "填报流程驳回",nickname = "驳回" )
    public  ResponseData<String> reject(@RequestBody RejectVO rejectVO){
        return  remoteReportFolwFeginApi.reject(rejectVO);
    }
    @ApiOperation( value = "子流程列表",nickname = "子流程列表" )
    @GetMapping({"/subTask/{taskId}"})
    public  ResponseData<OtherFillVO> subTask(@PathVariable("taskId") Integer taskId){
            return  remoteReportMobileApi.subTask(taskId);
    }

    @Autowired
   private RemoteReportExportFeign remoteReportExportFeign;


    @PostMapping({"/export/file"})
    @ApiOperation("预览表单（pdf文件的形式）")
    public void execl(@RequestBody ReportExportVO reportExportVO, HttpServletResponse response){
        ResponseData<byte[]> responseData = remoteReportExportFeign.export( reportExportVO);
        /*try {
            response.getOutputStream().write(test.getData());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }*/
        ServletOutputStream outputStream =null;
        try {
            outputStream = response.getOutputStream();
            outputStream.write(responseData.getData());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @ApiOperation(value = "pdf预览", nickname = "pdf预览")
    @PostMapping(value = "/export/pdf")
   public void exportPdf(@RequestBody ReportExportVO reportExportVO, HttpServletResponse response){
        ResponseData<byte[]> responseData = remoteReportExportFeign.exportPdf(reportExportVO);
        ServletOutputStream outputStream =null;
        try {
            outputStream = response.getOutputStream();
            outputStream.write(responseData.getData());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @ApiOperation(value = "文件预览（url形式）", nickname = "文件预览（url形式）")
    @PostMapping("/export/url")
    public ResponseData<String> exportUrl(@RequestBody  ReportExportVO reportExportVO){
        return remoteReportExportFeign.exportUrl(reportExportVO);

    }

    @Autowired
    private RemoteReportPalnApi remoteReportPalnApi;
    @ApiOperation(value = "通知辅助接口", nickname = "通知辅助接口")
    @GetMapping("/notice/{taskId}")
    public ResponseData<NoticeVo> notice(@PathVariable("taskId") Long taskId){
        return remoteReportPalnApi.notice(taskId);
    }



}
